﻿<%@ Page Title="" Language="C#" MasterPageFile="Site.Master" AutoEventWireup="true"
    CodeBehind="QueryBuilder.aspx.cs" Inherits="ISS.IR.RRNMS.Web.QueryBuilderPage" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#accordion").accordion({
                heightStyle: "content"
            });
        });

        function alertDateValue() {
            var x = document.getElementById('MainContent_txtDate');

            document.getElementById('MainContent_hdnDateValue').value = x.value;
        }

        function validateBeforeAddingConditions(ev, panelId) {
            if (document.getElementById('ddlEntities').selectedIndex == 0) {
                alert("من فضلك أختر الكيان المراد الاستعلام عنه");
            }
            else
                showModalPopupViaClient(ev, panelId);
        }

        function validateBeforeSavingView(ev, panelId) {
            if (document.getElementById('grdConditions') == null) {
                alert("من فضلك أضف شرط واحد على الأقل");
                return false;
            }
            else
                showModalPopupViaClient(ev, panelId);
        }

        function validateBeforeSearch() {
            if (document.getElementById('grdConditions') == null) {
                alert("من فضلك أضف شرط واحد على الأقل");
                return false;
            }
            else
                return true;
        }

        function showModalPopupViaClient(ev, panelId) {
            ev.preventDefault();
            var modalPopupBehavior = $find(panelId);
            modalPopupBehavior.show();
        }

        function hideModalPopupViaClient(ev, panelId) {
            ev.preventDefault();
            var modalPopupBehavior = $find(panelId);
            modalPopupBehavior.hide();
        }

        function hideModalPopupViaClient_add(ev, panelId) {
            ev.preventDefault();
            var modalPopupBehavior = $find(panelId);
            modalPopupBehavior.hide();
            alert(1);
        }
    </script>
    <style type="text/css">
        .modalBackground
        {
            background-color: Gray;
            filter: alpha(opacity=70);
            opacity: 0.7;
        }
        
        .modalPopup
        {
            background-color: #ffffdd;
            border-width: 3px;
            border-style: solid;
            border-color: Gray;
            padding: 3px;
            width: 400px;
        }
        
        .modalPopup p
        {
            padding: 5px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:HiddenField ID="hiddenViewId" runat="server" ClientIDMode="Static" />
    <table width="100%" align="center">
        <tr>
            <td width="20%">
                <asp:Label runat="server" ID="lblEntities" Text="استعلام عن" />
            </td>
            <td width="80%">
                <asp:DropDownList runat="server" ID="ddlEntities" Width="25%" OnSelectedIndexChanged="ddlEntities_SelectedIndexChanged"
                    AutoPostBack="true" ClientIDMode="Static">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <asp:Panel ID="pnlCondtions" runat="server" GroupingText="الشروط">
                    <asp:UpdatePanel ID="CondtionsGridUpdatePanel" runat="server">
                        <ContentTemplate>
                            <asp:GridView ID="grdConditions" runat="server" AutoGenerateColumns="False" Width="70%"
                                CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="grdConditions_RowCommand"
                                ClientIDMode="Static">
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                <Columns>
                                    <asp:TemplateField HeaderText="اسم الحقل" ControlStyle-Width="33.3%">
                                        <ItemTemplate>
                                            <asp:Label ID="lblFieldDisplayName" runat="server" Text='<%# Eval("FieldDisplayName") %>'
                                                Width="100%"></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Operator" ControlStyle-Width="33.3%">
                                        <ItemTemplate>
                                            <asp:Label ID="lblOperatorDisplayName" runat="server" Text='<%# Eval("OperatorDisplayName") %>'
                                                Width="100%"></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="القيمة" ControlStyle-Width="33.3%">
                                        <ItemTemplate>
                                            <asp:Label ID="lblValue" runat="server" Text='<%# Eval("Value") %>' Width="100%"></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:ImageButton runat="server" ID="btnDeleteCondition" ImageUrl="images/Action-cancel-icon.png"
                                                CommandName="DeleteQueryCondition" CommandArgument='<%# Container.DataItemIndex %>'
                                                CausesValidation="false" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <EditRowStyle BackColor="#999999" />
                                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                            </asp:GridView>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="lnkAddCondition" />
                        </Triggers>
                    </asp:UpdatePanel>
                </asp:Panel>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="left">
                <%--<asp:ImageButton ID="btnSaveView" runat="server" AlternateText="حفظ البحث الحالي" ImageUrl="/images/Save.png" OnClick="btnSaveView_Click" CausesValidation="false" />--%>
                <a id="aSaveView" href="#" onclick="validateBeforeSavingView(event,'programmaticModalPopupBehavior_SaveView')">
                    <img src="/images/Save.png" align="left" alt="حفظ البحث الحالي" /></a> <a id="aLoadView"
                        href="#" onclick="validateBeforeAddingConditions(event,'programmaticModalPopupBehavior_LoadView')">
                        <img src="/images/open.png" align="left" alt="تحميل بحث" /></a>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <asp:Button runat="server" ID="btnHiddenAlreadyRegistered" Style="display: none" />
                <ajaxToolkit:ModalPopupExtender runat="server" ID="programmaticModalPopup_AlreadyRegistered"
                    BehaviorID="programmaticModalPopupBehavior_AlreadyRegistered" TargetControlID="btnHiddenAlreadyRegistered"
                    PopupControlID="pnlAlreadyRegistered" BackgroundCssClass="modalBackground" DropShadow="True"
                    PopupDragHandleControlID="programmaticPopupDragHandle_AlreadyRegistered" RepositionMode="RepositionOnWindowScroll">
                </ajaxToolkit:ModalPopupExtender>
                <asp:Panel ID="pnlAlreadyRegistered" runat="server" CssClass="modalPopup" Style="display: none;">
                    <asp:Panel runat="Server" ID="programmaticPopupDragHandle_AlreadyRegistered" Style="cursor: move;
                        background-color: #DDDDDD; border: solid 1px Gray; color: Black; text-align: center;
                        height: 30px; vertical-align: middle;">
                        <a id="A2" href="#" onclick="hideModalPopupViaClient(event,'programmaticModalPopupBehavior_AlreadyRegistered')">
                            <img src="Images/Action-cancel-icon.png" align="left" alt="إغلاق النافذة" /></a>
                        <asp:Label ID="lblModalHeader" runat="server" Style="vertical-align: middle;" Text="شرط جديد"
                            Font-Size="X-Large" Font-Bold="True"></asp:Label>
                    </asp:Panel>
                    <table width="80%">
                        <tr>
                            <td width="30%">
                                <asp:Label ID="lblFieldName" runat="server" Text="الحقل" />
                            </td>
                            <td width="70%">
                                <asp:UpdatePanel ID="fieldsUpdatePanel" runat="server">
                                    <ContentTemplate>
                                        <asp:DropDownList ID="ddlFields" runat="server" Width="90%" AutoPostBack="true" OnSelectedIndexChanged="ddlFields_SelectedIndexChanged" />
                                        <asp:CompareValidator ID="fieldsValidator" runat="server" ControlToValidate="ddlFields"
                                            ValueToCompare="0" Operator="NotEqual" Type="Integer" ErrorMessage="*" ForeColor="Red"></asp:CompareValidator>
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="ddlEntities" />
                                    </Triggers>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                        <tr>
                            <td width="30%">
                                <asp:Label ID="lblOperator" runat="server" Text="Operator" />
                            </td>
                            <td width="70%">
                                <asp:UpdatePanel ID="operatorsUpdatePanel" runat="server">
                                    <ContentTemplate>
                                        <asp:DropDownList ID="ddlOperators" runat="server" Width="90%" />
                                        <asp:RequiredFieldValidator ID="operatorValidator" runat="server" ControlToValidate="ddlOperators"
                                            ErrorMessage="*" ForeColor="Red" />
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="ddlFields" />
                                    </Triggers>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                        <tr>
                            <td width="30%">
                                <asp:Label ID="lblValue" runat="server" Text="القيمة" />
                            </td>
                            <td width="70%">
                                <asp:UpdatePanel ID="ValuesUpdatePanel" runat="server">
                                    <ContentTemplate>
                                        <asp:Panel ID="pnlStaticValue" runat="server" BorderStyle="None" Visible="false">
                                            <asp:TextBox ID="txtValue" runat="server" Width="90%" />
                                            <asp:RequiredFieldValidator ID="textValueValidator" runat="server" ControlToValidate="txtValue"
                                                ErrorMessage="*" ForeColor="Red" />
                                        </asp:Panel>
                                        <asp:Panel ID="pnlDateValue" runat="server" BorderStyle="None" Visible="false">
                                            <asp:TextBox ID="txtDate" runat="server" Width="90%" TextMode="Date" onchange='alertDateValue()' />
                                            <asp:HiddenField ID="hdnDateValue" runat="server" />
                                            <asp:RequiredFieldValidator ID="dateValidator" runat="server" ControlToValidate="txtDate"
                                                ErrorMessage="*" ForeColor="Red" />
                                        </asp:Panel>
                                        <asp:Panel ID="pnlLookupValue" runat="server" BorderStyle="None" Visible="false">
                                            <asp:DropDownList ID="ddlLookupValue" runat="server" Width="90%" />
                                            <asp:RequiredFieldValidator ID="lookupValidator" runat="server" ControlToValidate="ddlLookupValue"
                                                ErrorMessage="*" ForeColor="Red" />
                                        </asp:Panel>
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="ddlFields" />
                                    </Triggers>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" align="center">
                                <asp:LinkButton ID="lnkAddCondition" runat="server" Text="أضف" OnClick="btnAddCondition_Click" />
                            </td>
                        </tr>
                    </table>
                </asp:Panel>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <a id="showModalPopupClientButton" href="#" onclick="validateBeforeAddingConditions(event,'programmaticModalPopupBehavior_AlreadyRegistered')">
                    أضف شرط جديد</a>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <asp:ImageButton ID="btnSearch" AlternateText="بحث" runat="server" ImageUrl="images/search.png"
                    OnClientClick="validateBeforeSearch()" OnClick="btnSearch_Click" CausesValidation="false" />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <asp:UpdatePanel ID="resultsUpdatePanel" runat="server">
                    <ContentTemplate>
                        <asp:Panel ID="pnlResult" runat="server" GroupingText="نتيجة البحث">
                            <asp:GridView ID="gvAppointment" runat="server" CellPadding="4" ForeColor="#333333"
                                GridLines="None" AutoGenerateColumns="false">
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                <EditRowStyle BackColor="#999999" />
                                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                                <Columns>
                                    <asp:TemplateField HeaderText="اللاجئ">
                                        <ItemTemplate>
                                            <asp:HyperLink ID="hlAccountName" runat="server" Text='<%# Eval("AccountName") %>'
                                                NavigateUrl='<%# "Account.aspx?id=" + Eval("AccountId")%>' Target="_blank" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="UserName" HeaderText="المستخدم" />
                                    <asp:BoundField DataField="RoomName" HeaderText="الحجرة" />
                                    <asp:BoundField DataField="SlotName" HeaderText="الفترة" />
                                    <asp:BoundField DataField="Date" HeaderText="التاريخ" DataFormatString="{0:dd/MM/yyyy}"
                                        HtmlEncode="false" />
                                </Columns>
                            </asp:GridView>
                            <asp:GridView ID="gvAccounts" runat="server" CellPadding="4" ForeColor="#333333"
                                GridLines="None" AutoGenerateColumns="false">
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                <EditRowStyle BackColor="#999999" />
                                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                                <Columns>
                                    <asp:TemplateField HeaderText="اللاجئ">
                                        <ItemTemplate>
                                            <asp:HyperLink ID="hlAccountName" runat="server" Text='<%# Eval("FullName") %>' NavigateUrl='<%# "Account.aspx?id=" + Eval("AccountId")%>'
                                                Target="_blank" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="UNHCR" HeaderText="الرقم" />
                                </Columns>
                            </asp:GridView>
                            <br />
                            <asp:ImageButton ID="btnExportToExcel" runat="server" ImageUrl="images/Excel-icon.png"
                                OnClick="btnExportToExcel_Click" CausesValidation="false" />
                        </asp:Panel>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="btnSearch" />
                        <asp:AsyncPostBackTrigger ControlID="ddlEntities" />
                        <asp:PostBackTrigger ControlID="btnExportToExcel" />
                    </Triggers>
                </asp:UpdatePanel>
            </td>
        </tr>
    </table>
    <table width="100%" align="center">
        <tr>
            <td align="center">
                <asp:Button runat="server" ID="btnHiddenLoadView" Style="display: none" />
                <ajaxToolkit:ModalPopupExtender runat="server" ID="ModalPopupExtender1" BehaviorID="programmaticModalPopupBehavior_LoadView"
                    TargetControlID="btnHiddenLoadView" PopupControlID="pnlLoadView" BackgroundCssClass="modalBackground"
                    DropShadow="True" PopupDragHandleControlID="programmaticPopupDragHandle_LoadView"
                    RepositionMode="RepositionOnWindowScroll">
                </ajaxToolkit:ModalPopupExtender>
                <asp:Panel ID="pnlLoadView" runat="server" CssClass="modalPopup" Style="display: none;">
                    <asp:Panel runat="Server" ID="Panel2" Style="cursor: move; background-color: #DDDDDD;
                        border: solid 1px Gray; color: Black; text-align: center; height: 30px; vertical-align: middle;">
                        <a id="A1" href="#" onclick="hideModalPopupViaClient(event,'programmaticModalPopupBehavior_LoadView')">
                            <img src="Images/Action-cancel-icon.png" align="left" alt="إغلاق النافذة" /></a>
                        <asp:Label ID="Label1" runat="server" Style="vertical-align: middle;" Text="اختيار البحث"
                            Font-Size="X-Large" Font-Bold="True"></asp:Label>
                    </asp:Panel>
                    <table width="100%">
                        <tr>
                            <td align="center">
                                <asp:UpdatePanel ID="searchViewsUpdatePanel" runat="server">
                                    <ContentTemplate>
                                        <asp:DropDownList ID="ddlSearchViews" runat="server" Width="90%" />
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="ddlEntities" />
                                    </Triggers>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                                <asp:LinkButton ID="lnkLoadView" runat="server" Text="اختيار" OnClick="lnkLoadView_Click"
                                    CausesValidation="false" />
                            </td>
                        </tr>
                    </table>
                </asp:Panel>
            </td>
        </tr>
    </table>
    <table width="100%" align="center">
        <tr>
            <td align="center">
                <asp:Button runat="server" ID="btnHiddenSaveView" Style="display: none" />
                <ajaxToolkit:ModalPopupExtender runat="server" ID="ModalPopupExtender2" BehaviorID="programmaticModalPopupBehavior_SaveView"
                    TargetControlID="btnHiddenSaveView" PopupControlID="pnlSaveView" BackgroundCssClass="modalBackground"
                    DropShadow="True" PopupDragHandleControlID="programmaticPopupDragHandle_SaveView"
                    RepositionMode="RepositionOnWindowScroll">
                </ajaxToolkit:ModalPopupExtender>
                <asp:Panel ID="pnlSaveView" runat="server" CssClass="modalPopup" Style="display: none;">
                    <asp:Panel runat="Server" ID="Panel3" Style="cursor: move; background-color: #DDDDDD;
                        border: solid 1px Gray; color: Black; text-align: center; height: 30px; vertical-align: middle;">
                        <a id="A3" href="#" onclick="hideModalPopupViaClient(event,'programmaticModalPopupBehavior_SaveView')">
                            <img src="Images/Action-cancel-icon.png" align="left" alt="إغلاق النافذة" /></a>
                        <asp:Label ID="Label2" runat="server" Style="vertical-align: middle;" Text="حفظ البحث الحالي"
                            Font-Size="X-Large" Font-Bold="True"></asp:Label>
                    </asp:Panel>
                    <table width="100%">
                        <tr>
                            <td align="center">
                                <asp:TextBox ID="txtViewName" runat="server" Width="90%" />
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                                <asp:LinkButton ID="lnkSaveView" runat="server" Text="حفظ" OnClick="lnkSaveView_Click"
                                    CausesValidation="false" />
                            </td>
                        </tr>
                    </table>
                </asp:Panel>
            </td>
        </tr>
    </table>
</asp:Content>
